//
//  Address.m
//  CarTown
//
//  Created by xionghao on 15/9/2.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "Address.h"
#import "AppDelegate.h"

@implementation Address

@dynamic area_name;
@dynamic area_id;
@dynamic parent_id;
+(void)addressWithDict:(NSArray *)add{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Address"];
    
   NSArray *array= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    for (Address *adress in array) {
        [delegate.managedObjectContext deleteObject:adress];
    }
    
    [Address objectArrayWithKeyValuesArray:add context:delegate.managedObjectContext];
    [delegate saveContext];
   
}

+(NSArray *)getPro{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Address"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"parent_id = 1"];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"area_name" ascending:YES];
    request.sortDescriptors=@[sort];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    return sets;
}
+(NSArray *)getCity:(NSString *)pid{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Address"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"parent_id =%@",pid]];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"area_name" ascending:YES];
    request.sortDescriptors=@[sort];

    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    return sets;
}
+(NSArray *)getDisct:(NSNumber *)pid{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Address"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"parent_id =%@",pid]];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"area_name" ascending:YES];
    request.sortDescriptors=@[sort];

    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    return sets;
}

+(instancetype)getAdressbyID:(NSString *)area_id{
    AppDelegate *delegate= [[UIApplication sharedApplication] delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Address"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"area_id =%@",area_id]];
    request.predicate=pre;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"area_id" ascending:YES];
    request.sortDescriptors=@[sort];

    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    
    if (sets.count>0) {
        return sets[0];
    }else{
        return nil;
    }
}

@end
